package com.ruipengkj.view.giftView;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.commons.util.SwingUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.view.base.BaseInternalFrame;



public class GiftInternalFrame extends BaseInternalFrame {
	
	private static final long serialVersionUID = 1L;
	
	private JToolBar toolBar;
	
	private JPanel main;
	private CardLayout main_layout;
	
	private JButton createGiftButton;
	private GiftCreatePanel giftCreatePanel;
	private JButton giftButton;
	private GiftPanel giftPanel;

	

	public GiftInternalFrame(String title, boolean resizable, boolean closable,boolean maximizable, boolean iconifiable) {
		super(title, resizable, closable, maximizable, iconifiable);
		this.init();
	}

	private void init() {
		this.initViews();
		this.initListener();
	}

	private void initViews() {
		toolBar = new JToolBar();
		toolBar.setFloatable(false);
		
		createGiftButton = new JButton("添加礼品",SwingUtil.getOpenSwingImage("member_card_the issuing.png"));
		giftButton = new JButton("添加礼品信息",SwingUtil.getOpenSwingImage("member_card_the issuing.png"));
		if(ApplicationContext.TRIGHT_LIST.contains(ApplicationContext.RIGHT_LIVEL91)){
			toolBar.add(createGiftButton);
		}
		if(ApplicationContext.TRIGHT_LIST.contains(ApplicationContext.RIGHT_LIVEL92)){
			toolBar.add(giftButton);
		}
		
		main=new JPanel();
		main_layout=new CardLayout();
		main.setLayout(main_layout);
		this.add(toolBar,BorderLayout.NORTH);
		this.add(main,BorderLayout.CENTER);
	}
	
	private void initListener() {
		createGiftButton.addActionListener(this);
		giftButton.addActionListener(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==createGiftButton){
			giftCreatePanel=new GiftCreatePanel();
			main.add(giftCreatePanel,"giftCreatePanel");
			main_layout.show(main, "giftCreatePanel");
			this.setTitle("礼品管理--添加礼品");
			Util.setButtonStatus(toolBar,createGiftButton);
		}else if(e.getSource()==giftButton){
			giftPanel=new GiftPanel();
			main.add(giftPanel,"giftPanel");
			main_layout.show(main, "giftPanel");
			this.setTitle("礼品管理--礼品列表");
			Util.setButtonStatus(toolBar,giftButton);
		}
		main.updateUI();
	}
	
	public void showButton(){
		if (toolBar != null && toolBar.getComponentCount() != 0)
			((JButton)toolBar.getComponent(0)).doClick();
	}
	
	public void showButton(int type){
		switch(type){
			case 1:{createGiftButton.doClick();break;}
		}
	}
}
